//
//  LeaderModifyPasswordVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/9/1.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "LeaderModifyPasswordVC.h"
#import "LoginVC.h"
@interface LeaderModifyPasswordVC ()

@end

@implementation LeaderModifyPasswordVC
{
    UITextField *_oldPwTf;
    UITextField *_newPassTf;
    UITextField *_reNewPassTf;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.view.backgroundColor = White_Color;
    [self initView];
}
- (void)initView{
    _oldPwTf = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, KScreenWidth - 40, 40)];
    _oldPwTf.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _oldPwTf.leftViewMode = UITextFieldViewModeAlways;
    _oldPwTf.layer.cornerRadius = 5;
    _oldPwTf.layer.borderWidth = 1;
    _oldPwTf.layer.borderColor = blackLineColore.CGColor;
    _oldPwTf.placeholder = @"请输入旧密码";
    _oldPwTf.secureTextEntry = YES;
    [self.view addSubview:_oldPwTf];
    
    _newPassTf = [[UITextField alloc]initWithFrame:CGRectMake(20, 160, KScreenWidth - 40, 40)];
    _newPassTf.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _newPassTf.leftViewMode = UITextFieldViewModeAlways;
    _newPassTf.layer.cornerRadius = 5;
    _newPassTf.layer.borderWidth = 1;
    _newPassTf.layer.borderColor = blackLineColore.CGColor;
    _newPassTf.placeholder = @"请输入新密码";
    _newPassTf.secureTextEntry = YES;
    [self.view addSubview:_newPassTf];
    
    _reNewPassTf = [[UITextField alloc]initWithFrame:CGRectMake(20, 210, KScreenWidth - 40, 40)];
    _reNewPassTf.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _reNewPassTf.leftViewMode = UITextFieldViewModeAlways;
    _reNewPassTf.layer.cornerRadius = 5;
    _reNewPassTf.layer.borderWidth = 1;
    _reNewPassTf.layer.borderColor = blackLineColore.CGColor;
    _reNewPassTf.placeholder = @"请再次输入新密码";
    _reNewPassTf.secureTextEntry = YES;
    [self.view addSubview:_reNewPassTf];
    
    UIButton *doneBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 300, KScreenWidth - 40, 40)];
    doneBtn.backgroundColor = Theme_Color;
    doneBtn.layer.cornerRadius = 5;
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    doneBtn.tintColor = [UIColor whiteColor];
    [doneBtn addTarget:self action:@selector(doneBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:doneBtn];

}
- (void)doneBtnAction{
    //参数
    if ([_newPassTf.text isEqualToString:_reNewPassTf.text]) {
        NSDictionary *parameters = @{
                                     @"phone":[NSString readToNSDefaultsWithKey:kPhone],
                                     @"oldPass": _oldPwTf.text,
                                     @"pass":_newPassTf.text,
                                     };
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager POST:@"http://115.28.152.130/tour/v1/user/editUserPass.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
            NSDictionary *_responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@>>>>",_responseDic);
//            NSString *error = _responseDic[@"code"];
            if ([_responseDic[@"code"] isEqualToString:@"4001"]) {
                [SVProgressHUD showErrorWithStatus:@"原密码错误"];
            }else{
                //请求成功
                [SVProgressHUD showSuccessWithStatus:@"修改成功，请重新登录"];
                LoginVC *vc = [[LoginVC alloc]init];
                [self presentViewController:vc animated:NO completion:nil];
            }
            
        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"修改失败"];
        }];
    }else{
        [SVProgressHUD showErrorWithStatus:@"两次密码不一致"];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
